/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BUS.DichVu_BUS;
import BUS.MonAn_BUS;
import DTO.DichVu_DTO;
import DTO.MonAn_DTO;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author PEKEM
 */
public class ThemDichVu extends javax.swing.JFrame {

    String maDichVu;
    String maMonAn;

    /**
     * Creates new form ThemDichVu
     */
    public ThemDichVu() throws Exception {
        initComponents();
        showBangDV();
        showBangMA();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tbDichVu = new javax.swing.JTable();
        btThemDV = new javax.swing.JButton();
        btCapNhatDV = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tfTenDV = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfDonGiaDV = new javax.swing.JTextField();
        btXoaDV = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbMonAn = new javax.swing.JTable();
        btThemMA = new javax.swing.JButton();
        btCapNhatMA = new javax.swing.JButton();
        tfTenMA = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfDonGiaMA = new javax.swing.JTextField();
        btXoaMA = new javax.swing.JButton();

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Thêm");

        jTabbedPane2.setPreferredSize(new java.awt.Dimension(303, 165));

        tbDichVu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDichVu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDichVuMouseClicked(evt);
            }
        });
        jScrollPane5.setViewportView(tbDichVu);

        btThemDV.setText("Thêm Dịch Vụ");
        btThemDV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemDVActionPerformed(evt);
            }
        });

        btCapNhatDV.setText("Cập Nhật");
        btCapNhatDV.setEnabled(false);
        btCapNhatDV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatDVActionPerformed(evt);
            }
        });

        jLabel1.setText("Tên Dịch Vụ");

        jLabel2.setText("Đơn giá");

        tfDonGiaDV.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfDonGiaDV.setText("0");

        btXoaDV.setText("Xóa");
        btXoaDV.setEnabled(false);
        btXoaDV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaDVActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfTenDV, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tfDonGiaDV, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(75, 75, 75))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(139, 139, 139)
                        .addComponent(btThemDV)
                        .addGap(19, 19, 19)
                        .addComponent(btXoaDV, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btCapNhatDV, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 510, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCapNhatDV, btThemDV, btXoaDV});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfTenDV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(tfDonGiaDV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCapNhatDV)
                    .addComponent(btThemDV)
                    .addComponent(btXoaDV))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btCapNhatDV, btThemDV, btXoaDV});

        jTabbedPane2.addTab("Thêm Dịch Vụ", jPanel3);

        tbMonAn.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbMonAn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbMonAnMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tbMonAn);

        btThemMA.setText("Thêm Món Ăn");
        btThemMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemMAActionPerformed(evt);
            }
        });

        btCapNhatMA.setText("Cập Nhật");
        btCapNhatMA.setEnabled(false);
        btCapNhatMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatMAActionPerformed(evt);
            }
        });

        jLabel3.setText("Tên Món Ăn");

        jLabel4.setText("Đơn giá");

        tfDonGiaMA.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfDonGiaMA.setText("0");

        btXoaMA.setText("Xóa");
        btXoaMA.setEnabled(false);
        btXoaMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaMAActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btThemMA)
                        .addGap(18, 18, 18)
                        .addComponent(btCapNhatMA, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btXoaMA, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfTenMA, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(34, 34, 34)
                        .addComponent(tfDonGiaMA, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(33, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 504, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfTenMA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(tfDonGiaMA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCapNhatMA)
                    .addComponent(btThemMA)
                    .addComponent(btXoaMA))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(42, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Thêm Món Ăn", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 532, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCapNhatMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatMAActionPerformed
        // TODO add your handling code here:
        String tenMonAn;
        tenMonAn = tfTenMA.getText();
        if (tenMonAn.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên món ăn!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaMA.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaMA.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!!");
            tfDonGiaMA.setText("0");
            return;
        }
        try {
            MonAn_DTO ma = new MonAn_DTO();
            ma.setMaMonAn(maMonAn);
            ma.setTenMonAn(tenMonAn);
            ma.setDonGia(donGia.toString());
            MonAn_BUS.Update(ma);
            showBangMA();
        } catch (Exception ex) {
            Logger.getLogger(ThemDichVu.class.getName()).log(Level.SEVERE, null, ex);
        }
        tfTenMA.setText("");
        tfDonGiaMA.setText("0");
        btXoaMA.setEnabled(false);
        btCapNhatMA.setEnabled(false);
    }//GEN-LAST:event_btCapNhatMAActionPerformed

    private void btCapNhatDVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatDVActionPerformed
        // TODO add your handling code here:
        String tenDichVu;
        tenDichVu = tfTenDV.getText();
        if (tenDichVu.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên dịch vụ!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaDV.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaDV.setText("0");
                return;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!!");
            tfDonGiaDV.setText("0");
            return;
        }
        try {
            DichVu_DTO dv = new DichVu_DTO();
            dv.setMaDichVu(maDichVu);
            dv.setTenDichVu(tenDichVu);
            dv.setDonGia(donGia.toString());
            DichVu_BUS.Update(dv);
            showBangDV();
        } catch (Exception ex) {
            Logger.getLogger(ThemDichVu.class.getName()).log(Level.SEVERE, null, ex);
        }
        tfTenDV.setText("");
        tfDonGiaDV.setText("0");
        btXoaDV.setEnabled(false);
        btCapNhatDV.setEnabled(false);
    }//GEN-LAST:event_btCapNhatDVActionPerformed

    private void btThemMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemMAActionPerformed
        // TODO add your handling code here:
        try {
            luuMonAn();
            showBangMA();
        } catch (Exception ex) {
            Logger.getLogger(ThemMonAn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btThemMAActionPerformed

    private void btThemDVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemDVActionPerformed
        // TODO add your handling code here:
        try {
            luuDichVu();
            showBangDV();
        } catch (Exception ex) {
            Logger.getLogger(ThemMonAn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btThemDVActionPerformed

    private void btXoaMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaMAActionPerformed
        // TODO add your handling code here:
        int row = this.tbMonAn.getSelectedRow();
        int xoa = JOptionPane.showConfirmDialog(rootPane, "Bạn có chắc chắn muốn xóa?");
        if (xoa != 0) {
            return;
        }
        try {
            String maMonAnXoa = (String) this.tbMonAn.getValueAt(row, 0).toString();
            if(MonAn_BUS.kiemTraMonAnCoTrongCTPhieuDatTiec(maMonAnXoa)){
                JOptionPane.showMessageDialog(rootPane, "Món ăn đã được đặt tiệc không thể xóa!");
                return;
            }
            MonAn_BUS.Delete(maMonAnXoa);
            showBangMA();
        } catch (Exception e) {
        }
        tfTenMA.setText("");
        tfDonGiaMA.setText("0");
        btXoaMA.setEnabled(false);
        btCapNhatMA.setEnabled(false);
    }//GEN-LAST:event_btXoaMAActionPerformed

    private void tbDichVuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDichVuMouseClicked
        // TODO add your handling code here:
        int row = tbDichVu.getSelectedRow();
        if (row >= 0) {
            btXoaDV.setEnabled(true);
            btCapNhatDV.setEnabled(true);
            maDichVu = (String) tbDichVu.getValueAt(row, 0);
            String tenDichVu = (String) tbDichVu.getValueAt(row, 1);
            String donGia = tbDichVu.getValueAt(row, 2).toString();
            tfTenDV.setText(tenDichVu);
            tfDonGiaDV.setText(donGia);
        }
    }//GEN-LAST:event_tbDichVuMouseClicked

    private void btXoaDVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaDVActionPerformed
        // TODO add your handling code here:
        int row = this.tbDichVu.getSelectedRow();
        int xoa = JOptionPane.showConfirmDialog(rootPane, "Bạn có chắc chắn muốn xóa?");
        if (xoa != 0) {
            return;
        }
        try {
            String maDichVuXoa = (String) this.tbDichVu.getValueAt(row, 0).toString();
            if(DichVu_BUS.kiemTraDichVuCoTrongCTPhieuDichVu(maDichVuXoa)){
                JOptionPane.showMessageDialog(rootPane, "Dịch vụ đã được đặt không thể xóa!");
                return;
            }
            DichVu_BUS.Delete(maDichVuXoa);
            showBangDV();
        } catch (Exception e) {
        }
        tfTenDV.setText("");
        tfDonGiaDV.setText("0");
        btXoaDV.setEnabled(false);
        btCapNhatDV.setEnabled(false);
    }//GEN-LAST:event_btXoaDVActionPerformed

    private void tbMonAnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbMonAnMouseClicked
        // TODO add your handling code here:
        int row = tbMonAn.getSelectedRow();
        if (row >= 0) {
            btXoaMA.setEnabled(true);
            btCapNhatMA.setEnabled(true);
            maMonAn = (String) tbMonAn.getValueAt(row, 0);
            String tenMonAn = (String) tbMonAn.getValueAt(row, 1);
            String donGia = tbMonAn.getValueAt(row, 2).toString();
            tfTenMA.setText(tenMonAn);
            tfDonGiaMA.setText(donGia);
        }
    }//GEN-LAST:event_tbMonAnMouseClicked

    private void showBangMA() throws Exception {
        tbMonAn.setModel(MonAn_BUS.layBangMonAn());
    }

    public void luuMonAn() throws Exception {
        String tenMonAn;
        tenMonAn = tfTenMA.getText();
        if (tenMonAn.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên món ăn!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaMA.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaMA.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
            tfDonGiaMA.setText("0");
            return;
        }
        maMonAn = maMonAn();
        MonAn_DTO ma = new MonAn_DTO();
        ma.setMaMonAn(maMonAn);
        ma.setTenMonAn(tenMonAn);
        ma.setDonGia(donGia.toString());
        MonAn_BUS.Insert(ma);
        JOptionPane.showMessageDialog(rootPane, "Thêm món ăn thành công!");
        showBangMA();
//        ArrayList<MonAn_DTO> lst = new ArrayList();
//        tbMonAn.clearSelection();
//        int i = 0;
//        while (tbMonAn.getValueAt(i, 0) != null) {
//            MonAn_DTO mon = new MonAn_DTO();
//            try {
//                Float f = Float.parseFloat(tbMonAn.getValueAt(i, 1).toString());
//            } catch (Exception e) {
//                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số!");
//                return;
//            }
//            mon.setTenMonAn(tbMonAn.getValueAt(i, 0).toString());
//            mon.setDonGia(tbMonAn.getValueAt(i, 1).toString());
//            lst.add(mon);
//            //MonAn_BUS.Insert(mon);
//            i++;
//        }
//        for (MonAn_DTO m : lst) {
//            String maMon = maMonAn();
//            m.setMaMonAn(maMon);
//            MonAn_BUS.Insert(m);
//        }
//        lst.clear();
//        JOptionPane.showMessageDialog(rootPane, "Thêm món ăn thành công!");
//        tbMonAn.setModel(new DefaultTableModel(tableTitle, 7));
    }

    protected String maMonAn() throws Exception {
        String lastID = MonAn_BUS.layMaCuoiCung();
        String nextID = null;
        try {
            if (lastID != null) {
                nextID = MonAn_BUS.taoMaTuDong(lastID, 3);
            } else {
                nextID = MonAn_BUS.taoMaTuDong("MMA0000", 3);
            }
        } catch (Exception e) {
        }
        return nextID;
    }

    private void showBangDV() throws Exception {
        tbDichVu.setModel(DichVu_BUS.layBangDichVu());
    }

    public void luuDichVu() throws Exception {
        String tenDichVu;
        tenDichVu = tfTenDV.getText();
        if (tenDichVu.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên dịch vụ!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaDV.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaDV.setText("0");
                return;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!!");
            tfDonGiaDV.setText("0");
            return;
        }
        String maDichVu = maDichVu();
        DichVu_DTO dv = new DichVu_DTO();
        dv.setMaDichVu(maDichVu);
        dv.setTenDichVu(tenDichVu);
        dv.setDonGia(donGia.toString());
        DichVu_BUS.Insert(dv);
        JOptionPane.showMessageDialog(rootPane, "Thêm dịch vụ thành công!");

//        ArrayList<DichVu_DTO> lst = new ArrayList();
//        tbDichVu.clearSelection();
//        int i = 0;
//        while (tbDichVu.getValueAt(i, 0) != null) {
//            DichVu_DTO dv = new DichVu_DTO();
//            try {
//                Float f = Float.parseFloat(tbDichVu.getValueAt(i, 1).toString());
//            } catch (Exception e) {
//                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số!");
//                return;
//            }
//            dv.setTenDichVu(tbDichVu.getValueAt(i, 0).toString());
//            dv.setDonGia(tbDichVu.getValueAt(i, 1).toString());
//            lst.add(dv);
//            //MonAn_BUS.Insert(mon);
//            i++;
//        }
//        for (DichVu_DTO m : lst) {
//            String maDV = maDichVu();
//            m.setMaDichVu(maDV);
//            DichVu_BUS.Insert(m);
//        }
//        lst.clear();
//        JOptionPane.showMessageDialog(rootPane, "Thêm dịch vụ thành công!");
//        tbDichVu.setModel(new DefaultTableModel(tableTitle, 7));
    }

    protected String maDichVu() throws Exception {
        String lastID = DichVu_BUS.layMaCuoiCung();
        String nextID = null;
        try {
            if (lastID != null) {
                nextID = DichVu_BUS.taoMaTuDong(lastID, 3);
            } else {
                nextID = DichVu_BUS.taoMaTuDong("MDV0000", 3);
            }
        } catch (Exception e) {
        }
        return nextID;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThemDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThemDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThemDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThemDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ThemDichVu().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(ThemDichVu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhatDV;
    private javax.swing.JButton btCapNhatMA;
    private javax.swing.JButton btThemDV;
    private javax.swing.JButton btThemMA;
    private javax.swing.JButton btXoaDV;
    private javax.swing.JButton btXoaMA;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable tbDichVu;
    private javax.swing.JTable tbMonAn;
    private javax.swing.JTextField tfDonGiaDV;
    private javax.swing.JTextField tfDonGiaMA;
    private javax.swing.JTextField tfTenDV;
    private javax.swing.JTextField tfTenMA;
    // End of variables declaration//GEN-END:variables
}
